<%@page import="com.module.admin.tts.enums.JobStatus"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/view/inc/sys.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${projectName}-定时任务-项目管理</title>
<jsp:include page="/WEB-INF/view/inc/css.jsp"></jsp:include>
</head>
<body>
	<jsp:include page="/WEB-INF/view/inc/header.jsp"></jsp:include>
	<div class="container">
		<jsp:include page="/WEB-INF/view/admin/comm/left.jsp">
			<jsp:param name="first" value="tts"/>
			<jsp:param name="second" value="projectManager"/>
		</jsp:include>
		<div class="c-right">
			<div class="${bodyMainClass}">
				<div class="panel-heading panel-heading-tool">
					<div class="row">
						<div class="col-sm-5 title">定时任务 / <b>项目管理</b></div>
						<div class="col-sm-7 text-right">
							<div class="btn-group">
						  		<a href="javascript:location.reload()" class="btn btn-default btn-sm">刷新</a>
							</div>
						</div>
					</div>
				</div>
				<div class="panel-body">
					<div class="row layout">
				  		<div class="col-sm-4 left">
				  			<div class="table-tool-panel">
				  			<div class="row">
								<div class="col-sm-8">
									<span class="enter-panel">
										<input type="text" class="form-control input-sm w-120" id="projectName" placeholder="项目名称">
							  			<button type="button" class="btn btn-sm btn-default enter-fn" onclick="info.loadInfo(1)">查询</button>
							  		</span>
								</div>
								<div class="col-sm-4 text-right">
								  	<div class="btn-group">
									  	<my:auth mid="projectManagerAdd"><a href="javascript:;" class="btn btn-success btn-sm" onclick="info.edit()">+项目</a></my:auth>
								  	</div>
								</div>
							</div>
							</div>
							<div id="infoPanel" class="table-panel"></div>
							<div id="infoPage" class="table-page-panel"></div>
				  		</div>
				  		<div class="col-sm-8 right">
				  			<div id="rightInfo" class="right-panel">
					  			<div class="table-tool-panel">
						  			<div class="row">
										<div class="col-sm-8">
											<span class="enter-panel">
												<input type="text" class="form-control input-sm w-120" id="jobName" placeholder="任务名称">
												<select id="servid" class="form-control input-sm w-100">
													<option value="">选择服务</option>
													<c:forEach items="${servInfos}" var="info"><option value="${info.servid}">${info.servid}</option></c:forEach>
												</select>
										  		<button type="button" class="btn btn-sm btn-default enter-fn" onclick="right.loadInfo(1)">查询</button>
											  	<a href="javascript:;" class="btn btn-default btn-sm" onclick="right.exp()">导出</a>
									  		</span>
										</div>
										<div class="col-sm-4 text-right">
											<a class="text-success" href="${webroot}/help.jsp#taskInfo" target="_blank"><small>帮助</small>&nbsp;</a>
								  			<div class="btn-group">
											  	<my:auth mid="projectManagerJobAdd">
											  	<a href="javascript:;" class="btn btn-default btn-sm" onclick="right.imp()">导入</a>
											  	<a href="javascript:;" class="btn btn-success btn-sm" onclick="right.edit()">+任务</a>
											  	</my:auth>
										  	</div>
										</div>
									</div>
							  	</div>
								<div id="rightPanel" class="table-panel"></div>
								<div id="rightPage" class="table-page-panel"></div>
							</div>
				  		</div>
				  	</div>
				  	
				</div>
			</div>
		</div>
		<br clear="all">
	</div>
	<jsp:include page="/WEB-INF/view/inc/js.jsp"></jsp:include>
	<jsp:include page="/WEB-INF/view/inc/utils/page.jsp"></jsp:include>
<script type="text/javascript">
var infoPage = undefined;
var info = {
		//获取用户信息
		loadInfo : function(page) {
			if(!infoPage) {
				infoPage = new Page('infoPage', info.loadInfo, 'infoPanel', 'infoPage');
				infoPage.beginString = ['<table class="table table-striped table-hover"><thead><tr class="info">',
				                         '<th>编号</th>',
				                         '<th>项目名称</th>',
				                         '<th width="80">发邮件</th>',
				                         '<th width="60">操作</th>',
				                         '</tr></thead><tbody>'].join('');
				infoPage.endString = '</tbody></table>';
			}
			if(page != undefined)
				infoPage.page = page;

			JUtil.ajax({
				url : '${webroot}/ttsTaskProject/f-json/pageQuery',
				data : { page:infoPage.page, size:infoPage.size, name:$('#projectName').val() },
				beforeSend: function(){ infoPage.beforeSend('加载信息中...'); },
				error : function(json){ infoPage.error('加载信息出错了!'); },
				success : function(json){
					if(json.code === 0) {
						function getResult(obj) {
							var trClass = '';
							if(right.projectid === undefined) {
								right.projectid = obj.id;
								right.loadInfo(1);
								trClass = 'success';
							}
							return ['<tr class="',trClass,'" onclick="info.toRight(this,\'',obj.id,'\')">',
							    	'<td>',obj.id,'</td>',
							    	'<td>',obj.name,'</td>',
							    	'<td>',obj.isrecemailname,'</td>',
							    	'<td><my:auth mid="projectManagerUpd"><a class="glyphicon glyphicon-edit text-success" href="javascript:info.edit(',obj.id,')" title="修改"></a> </my:auth>',
							    	'<my:auth mid="projectManagerDel">&nbsp; <a class="glyphicon glyphicon-remove text-success" href="javascript:info.del(',obj.id,')" title="删除"></a></my:auth>',
							    	'</td>',
								'</tr>'].join('');
						}
						infoPage.operate(json.body, { resultFn:getResult, dataNull:'没有记录噢' });
					}
					else alert(JUtil.msg.ajaxErr);
				}
			});
		},
		edit : function(id) {
			dialog({
				title: '编辑项目',
				url: webroot + '/ttsTaskProject/f-view/edit?id='+(id?id:''),
				type: 'iframe',
				width: 620,
				height: 480
			});
		},
		del : function(id) {
			if(confirm('项目被删除后，其对应的任务都会被删除，您确定要删除该项目吗?')) {
				JUtil.ajax({
					url : '${webroot}/ttsTaskProject/f-json/delete',
					data : { id: id },
					success : function(json) {
						if (json.code === 0) {
							message('删除成功');
							info.loadInfo(1);
						}
						else if (json.code === -1)
							message(JUtil.msg.ajaxErr);
						else
							message(json.msg);
					}
				});
			}
		},
		toRight: function(_this, projectid) {
			var tr = $(_this);
			tr.parent().find('tr').each(function(i, obj) {
				$(obj).removeClass('success');
			});
			tr.addClass('success');
			right.projectid = projectid;
			right.loadInfo(1);
		}
};

var rightPage = undefined;
var right = {
		//获取用户信息
		loadInfo : function(page) {
			if(!rightPage) {
				rightPage = new Page('rightPage', right.loadInfo, 'rightPanel', 'rightPage');
				rightPage.beginString = ['<table class="table table-striped table-hover"><thead><tr class="info">',
                    					 '<th width="60"><input type="checkbox" onclick="right.jobChange(this)"/> 编号</th>',
				                         '<th>任务名称</th>',
				                         '<th>服务编号</th>',
				                         '<th width="70">状态</th>',
				                         '<th width="150">下次执行时间</th>',
				                         '<th width="130">操作</th>',
				                         '</tr></thead><tbody>'].join('');
				rightPage.endString = '</tbody></table>';
			}
			if(page != undefined)
				rightPage.page = page;

			JUtil.ajax({
				url : '${webroot}/ttsTaskJob/f-json/pageQuery',
				data : { page:rightPage.page, size:rightPage.size, projectid: right.projectid,
					name: $('#jobName').val(), servid: $('#servid').val()
				},
				beforeSend: function(){ rightPage.beforeSend('加载信息中...'); },
				error : function(json){ rightPage.error('加载信息出错了!'); },
				success : function(json){
					if(json.code === 0) {
						function getResult(obj) {
							var _operate = [];
							var _statusname = '<span class="label label-' + (obj.status === 0 ? 'success':'danger') + '">' + obj.statusname + '</span>';
							if(obj.status === 0 || obj.status === <%=JobStatus.WAIT.getCode()%>) {
								_operate.push('<my:auth mid="projectManagerJobUpd">&nbsp; <a href="javascript:right.status(',obj.id,',<%=JobStatus.STOP.getCode()%>)" class="glyphicon glyphicon-pause text-danger" title="暂停"></a></my:auth>');
							} else {
								_operate.push('<my:auth mid="projectManagerJobUpd">&nbsp; <a href="javascript:right.status(',obj.id,',<%=JobStatus.NORMAL.getCode()%>)" class="glyphicon glyphicon-play text-success" title="启动"></a></my:auth>');
							}
							_operate.push('<my:auth mid="projectManagerJobUpd">&nbsp; <a class="glyphicon glyphicon-edit text-success" href="javascript:right.edit(',obj.id,')" title="修改"></a></my:auth>',
							    			'<my:auth mid="projectManagerJobDel">&nbsp; <a class="glyphicon glyphicon-remove text-success" href="javascript:right.del(',obj.id,')" title="删除"></a></my:auth>',
							    			'&nbsp;&nbsp; <span class="dropdown opt-more">',
											'<a class="glyphicon text-success dropdown-toggle" href="javascript:;" data-toggle="dropdown">更多...</a>',
											'<ul class="dropdown-menu right" role="menu">',
											'<my:auth mid="projectManagerJobExec"><li role="presentation"><a href="javascript:right.execJob(',obj.id,')" title="立即执行改任务">立即执行</a></li></my:auth>',
											'<li role="presentation"><a href="',webroot,'/ttsTaskJobLog/f-view/manager?sourceType=job&projectid=',right.projectid,'&jobid=',obj.id,'" target="_blank">调度日志</a></li>',
											'</ul>',
											'</span>');
							return ['<tr>',
									'<td><input type="checkbox" name="jobs" value="',obj.id,'"/> ',obj.id,'</td>',
							    	'<td>',obj.name,'</td>',
							    	'<td>',obj.servid,'</td>',
							    	'<td>',_statusname,'</td>',
							    	'<td>',obj.nextValidTime,'</td>',
							    	'<td>',_operate.join(''),'</td>',
								'</tr>'].join('');
						}
						rightPage.operate(json.body, { resultFn:getResult, dataNull:'没有记录噢' });
						JUtil.sys.initOptMore();
					}
					else alert(JUtil.msg.ajaxErr);
				}
			});
		},
		jobChange: function(_this) {
			var bool = $(_this).prop('checked');
			$('input[name="jobs"]').each(function(i, item) {
				$(item).prop('checked', bool);
			});
		},
		//启动或停止任务
		status : function(id, status) {
			var _statusname = (status===0?'启动':'停止');
			if(confirm('您确定要'+_statusname+'该任务吗?')) {
				JUtil.ajax({
					url : '${webroot}/ttsTaskJob/f-json/updateStatus',
					data : { id: id, status: status },
					success : function(json) {
						if (json.code === 0) {
							message(_statusname + '成功');
							right.loadInfo(1);
						}
						else if (json.code === -1)
							message(JUtil.msg.ajaxErr);
						else
							message(json.msg);
					}
				});
			}
		},
		execJob : function(id) {
			if(confirm('您确定要立即执行该任务吗?')) {
				JUtil.ajax({
					url : '${webroot}/ttsTaskJob/f-json/execJob',
					data : { id: id },
					success : function(json) {
						if (json.code === 0) {
							message('执行成功');
							right.loadInfo();
						}
						else if (json.code === -1)
							message(JUtil.msg.ajaxErr);
						else
							message(json.msg);
					}
				});
			}
		},
		//编辑
		edit : function(id) {
			dialog({
				title: '编辑任务',
				url: webroot + '/ttsTaskJob/f-view/edit?projectid='+right.projectid+'&id='+(id?id:''),
				type: 'iframe',
				width: 520,
				height: 500
			});
		},
		exp : function(id) {
			var ids = [];
			$('input[name="jobs"]:checked').each(function(i, item) {
				ids.push($(item).val());
			});
			dialog({
				title: '编辑任务',
				url: webroot + '/ttsTaskJob/f-view/exp?projectid='+right.projectid+'&ids='+ids.join(','),
				type: 'iframe',
				width: 520,
				height: 380
			});
		},
		imp : function(id) {
			dialog({
				title: '导入任务',
				url: webroot + '/ttsTaskJob/f-view/imp?projectid='+right.projectid,
				type: 'iframe',
				width: 520,
				height: 380
			});
		},
		del : function(id) {
			if(confirm('您确定要删除该任务吗?')) {
				JUtil.ajax({
					url : '${webroot}/ttsTaskJob/f-json/delete',
					data : { id: id },
					success : function(json) {
						if (json.code === 0) {
							message('删除成功');
							right.loadInfo();
						}
						else if (json.result === -1)
							message(JUtil.msg.ajaxErr);
						else
							message(json.msg);
					}
				});
			}
		}
};
$(function() {
	info.loadInfo(1);
});
</script>
</body>
</html>